﻿namespace ScrumLite.Data
{
    using System;
    using System.Linq;
    using System.Collections.Generic;

    public class ReviewStore : ScrumLite.Data.IReviewStore
    {
        public Review[] RetrieveReviews(Guid projectId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                IQueryable<Review> query = from review in context.Reviews
                                               where review.fk_project == projectId
                                               orderby review.occuredOn descending
                                               select review;
                return query.ToArray();
            }
        }

        public Review RetrieveReview(Guid reviewId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                IQueryable<Review> query = from review in context.Reviews
                                               where review.id == reviewId
                                               select review;
                return query.FirstOrDefault();
            }            
        }

        public void Add(Review entity)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var review = new Review()
                {
                    id = entity.id,
                    occuredOn = entity.occuredOn,
                    fk_project = entity.fk_project,
                    conductedBy = entity.conductedBy,         
                    uri_evidence = entity.uri_evidence
                };

                context.Reviews.InsertOnSubmit(review);
                context.SubmitChanges();
            }
        }

        public void Update(Review entity)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var original = context.Reviews.SingleOrDefault(c => c.id == entity.id);
                if (original != null)
                {
                    original.fk_project = entity.fk_project;
                    original.occuredOn = entity.occuredOn;
                    original.conductedBy = entity.conductedBy;
                    original.uri_evidence = entity.uri_evidence;
                    context.SubmitChanges();
                }
            }
        }

        public Dictionary<DateTime, int> RetrieveDailyReviewCount(Guid projectId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var query = (from review in context.Reviews
                             where review.fk_project == projectId
                             group review by review.occuredOn into g
                             orderby g.Key
                             select new { Date = g.Key, Count = g.Count() });
                return query.ToDictionary(k => k.Date, v => v.Count);
            }
        }
    }
}

